//
//  RegisteredViewController.m
//  Recipe
//
//  Created by YaSha_Tom on 2018/12/17.
//  Copyright © 2018年 YaSha-Tom. All rights reserved.
//

#import "RegisteredViewController.h"
#import "RegisteredView.h"
#import <BmobSDK/Bmob.h>

@interface RegisteredViewController ()<UITextFieldDelegate>
@property (nonatomic, strong) RegisteredView *registeredView;
@property (nonatomic,strong) NSMutableDictionary *parameter;
@end

@implementation RegisteredViewController
- (RegisteredView *)loginView {
    if(!_registeredView){
        _registeredView = [[RegisteredView alloc]init];
        _registeredView.passworkFiled.delegate = self;
        _registeredView.nameFiled.delegate = self;
    }
    return _registeredView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"注册";
    self.view.backgroundColor = [UIColor whiteColor];
    self.parameter = [NSMutableDictionary dictionary];
    [self.view addSubview:self.loginView];
    @weakify(self);   //解决循环引用问题
    [[_registeredView.registeredButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        @strongify(self);
        [self doNetworking];
    }];
    
    [self monitorLogin];
}
# pragma -- 检测密码和用户名是否输入
- (void)monitorLogin {
    @weakify(self)
    //用户名输入监听信号
    RACSignal *validAccountSignal = [_registeredView.nameFiled.rac_textSignal map:^id(NSString  *value) {
        @strongify(self);
        [self.parameter setValue:value forKey:@"username"];
        return @(value.length > 0);
    }];
    
    //密码输入监听信号
    RACSignal *validPasswordSignal = [_registeredView.passworkFiled.rac_textSignal map:^id(NSString *value) {
        @strongify(self);
        [self.parameter setValue:value forKey:@"password"];
        return @(value.length > 0);
    }];
    RACSignal *loginButtonSignal = [RACSignal combineLatest:@[validAccountSignal, validPasswordSignal] reduce:^id(NSNumber *accountValid, NSNumber *passwordValid) {
        return @([accountValid boolValue] && [passwordValid boolValue]);
    }];
    
    RAC(_registeredView.registeredButton, backgroundColor) = [loginButtonSignal map:^id(id value) {
        @strongify(self);
        if ([value boolValue]) {
            self.registeredView.registeredButton.enabled = YES;
            return [UIColor qmui_colorWithHexString:@"F55931"];
        } else {
            self.registeredView.registeredButton.enabled = NO;
            return  [UIColor lightGrayColor];
        }
    }];
}

- (void)doNetworking {
    //在GameScore创建一条数据，如果当前没GameScore表，则会创建GameScore表
    BmobObject  *gameScore = [BmobObject objectWithClassName:@"userInfo"];
    NSLog(@"=====%@",self.parameter);
    [gameScore setObject:self.parameter[@"username"] forKey:@"userName"];
    [gameScore setObject:self.parameter[@"password"] forKey:@"passwork"];
    //异步保存到服务器
    [gameScore saveInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
        if (isSuccessful) {
            //创建成功后会返回objectId，updatedAt，createdAt等信息
            //创建对象成功，打印对象值
            NSLog(@"%@",gameScore);
            [self.navigationController popViewControllerAnimated:YES];
        } else if (error){
            //发生错误后的动作
            NSLog(@"%@",error);
        } else {
            NSLog(@"Unknow error");
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
